package enterprise.web.tookit.generic;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;

/**
 * 
 * @author regisrocha
 * 
 */
public class GenericDaoImpl implements GenericDao {

	/**
	 * Serial version
	 */
	private static final long serialVersionUID = -8027910765563655149L;

	/**
	 * Attribute for entity manager
	 */
	private EntityManager entityManager;

	public GenericDaoImpl(EntityManager em) {
		this.entityManager = em;
	}

	/**
	 * @author regisrocha
	 * 
	 * @since 2012
	 * 
	 * @description Make an entity instance managed and persistent.
	 */
	@Override
	public void persist(Object entity) {
		this.entityManager.persist(entity);
	}

	@Override
	public void remove(Object entity) {
		entityManager.remove(entity);

	}

	@Override
	public void flush() {
		entityManager.flush();

	}

	@Override
	public void setFlushMode(FlushModeType flushMode) {
		entityManager.setFlushMode(flushMode);

	}

	@Override
	public FlushModeType getFlushMode() {
		return entityManager.getFlushMode();
	}

	@Override
	public void lock(Object entity, LockModeType lockMode) {
		entityManager.lock(entity, lockMode);

	}

	@Override
	public void refresh(Object entity) {
		entityManager.refresh(entity);

	}

	@Override
	public void clear() {
		entityManager.clear();

	}

	@Override
	public boolean contains(Object entity) {
		return entityManager.contains(entity);
	}

	@Override
	public boolean isOpen() {
		return entityManager.isOpen();
	}

	@Override
	public EntityTransaction getTransaction() {
		return entityManager.getTransaction();
	}

	/**
	 * @return the entityManager
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * @author regisrocha
	 * 
	 * @since 2012
	 * 
	 * @description Merge the state of the given entity into the current persistence context.
	 * 
	 * @param T
	 * 
	 * @return T
	 */
	public <T> T merge(T t) {
		return entityManager.merge(t);
	}

	@Override
	public <T> T findByPrimaryKey(Class<T> entityClass, Object primaryKey) {
		return entityManager.find(entityClass, primaryKey);
	}
}
